package com.sheep.transer;

import org.apache.camel.CamelContext;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TranserMainApp {

    private static Logger log = Logger.getLogger(TranserMainApp.class);

    public static ApplicationContext springContext = null;
    public static CamelContext camelContext = null;

    /**
     * 启动服务
     *
     * @return
     */
    public static boolean startApp() {
        try {
            PropertyConfigurator.configure(System.getProperty("user.dir") + "/conf/log4j.properties");
            if (springContext == null) {
                springContext = new FileSystemXmlApplicationContext("conf/applicationContext.xml");
                camelContext = (CamelContext) springContext.getBean("muCamel");
            }
            camelContext.start();
        } catch (Exception e) {
            log.error("服务端启动失败...", e);
            return false;
        }
        log.info("********************服务端启动*******************");
//        waitForStop();
        new Thread(new Runnable() {
            @Override
            public void run() {
                waitForStop();
            }
        }).start();
        return true;
    }

    static void waitForStop() {
        while (true) {
            try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    /**
     * 停止服务
     *
     * @return
     */
    public static boolean stopApp() {
        try {
            log.info("********************服务端关闭*******************");
            camelContext.stop();
        } catch (Exception e) {
            log.error("服务端关闭失败...", e);
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        startApp();
    }


}
